﻿using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using System.DirectoryServices;
using System.Configuration;
using Texfina.Core.Common;
using Texfina.Core.Web;
using Texfina.Core.Data;
using Texfina.Entity.Sy;
using SYBOQry = Texfina.BOQry.Sy;

namespace SY
{
   public partial class wfUsuADBnd : SearchPage
   {
      public const int _GRDLIST_STANULADO = 6;

      protected void Page_Load(object sender, EventArgs e)
      {

         this.LoadInitialSettings();
         if (this.IsPostBack)
         {
            this.LoadInPostBack();
            return;
         }

         this.LoadData();
         this.SearchData();
      }

      public override void LoadInitialSettings()
      {
         Label lbl = (Label)this.Master.FindControl("lblTitle");
         lbl.Text = "Usuarios";
      }

      public override void SearchData()
      {
         DataTable dt = new DataTable();
         dt.Columns.Add("idLogon", Type.GetType("System.String"));
         dt.Columns.Add("dsNombres", Type.GetType("System.String"));
         dt.Columns.Add("dsApellidos", Type.GetType("System.String"));
         dt.Columns.Add("stAnulado", Type.GetType("System.String"));

         string strAdPath = "LDAP://" + ConfigurationManager.AppSettings[ConstSistema.APSET_AD_LDAP];

         DirectoryEntry entry = new DirectoryEntry(strAdPath);
         DirectorySearcher search = new DirectorySearcher(entry);

         search.Filter = "(objectCategory=person)";
         search.PropertiesToLoad.Add("cn");
         search.PropertiesToLoad.Add("objectGUID");
         search.PropertiesToLoad.Add("objectSID");
         search.PropertiesToLoad.Add("samAccountName");
         search.PropertiesToLoad.Add("givenname");
         search.PropertiesToLoad.Add("sn");

         SearchResultCollection results = search.FindAll();

         foreach (SearchResult r in results)
         {
            DataRow dr = dt.NewRow();

            dr.BeginEdit();
            dr["idLogon"] = r.Properties["samAccountName"][0].ToString();
            if (r.Properties["givenname"].Count > 0)
               dr["dsNombres"] = r.Properties["givenname"][0].ToString();
            else
               dr["dsNombres"] = "";

            if (r.Properties["sn"].Count > 0)
               dr["dsApellidos"] = r.Properties["sn"][0].ToString();
            else
               dr["dsApellidos"] = "";

            dr["stAnulado"] = ConstFlagEstado.DESACTIVADO;
            dr.EndEdit();

            dt.Rows.Add(dr);
         }

         this.GridSetDataBinding(grdList, dt);

      }

      protected void grdList_RowCreated(object sender, GridViewRowEventArgs e)
      {
         if (e.Row.RowType == DataControlRowType.DataRow)
            e.Row.Attributes.Add("ondblclick", "rowDblClick(" + e.Row.RowIndex.ToString() + ")");

      }

   }
}